//    Copyright (C) 2008 Dirk Vanden Boer <dirk.vdb@gmail.com>
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include "audiothumbnailer.h"
#include "overlay.h"

#include <iostream>
#include <sstream>
#include <stdexcept>
#include <assert.h>
#include <algorithm>

#include <taglib.h>
#include <fileref.h>
#include <tag.h>
#include <id3v2tag.h>
#include <mpegfile.h>
#include <attachedpictureframe.h>

#include <ImageMagick/Magick++.h>

using namespace std;
using namespace Magick;
using namespace TagLib::ID3v2;

static const int FRONT_COVER_TYPE = 3;

AudioThumbnailer::AudioThumbnailer(int thumbnailSize, int coverOverlay)
: m_ThumbnailSize(thumbnailSize)
, m_CoverOverlay(coverOverlay)
{
}

void AudioThumbnailer::generateThumbnail(const string& audioFile, const string& outputFile)
{
    TagLib::MPEG::File tagFile(audioFile.c_str());

    if (!tagFile.isValid() || !tagFile.ID3v2Tag())
    {
        cerr << "File does not contain ID3v2 Tag" << endl;
        return;
    }

    AttachedPictureFrame* pAlbumArt = NULL;

    FrameList list = tagFile.ID3v2Tag()->frameList("APIC");
    for (FrameList::Iterator iter = list.begin(); iter != list.end(); ++iter)
    {
        AttachedPictureFrame* pCurPicture = static_cast<AttachedPictureFrame*>(*iter);

        if (pCurPicture->type() == AttachedPictureFrame::FrontCover)
        {
            pAlbumArt = pCurPicture;
            break;
        }
        else if (pAlbumArt == NULL)
        {
            pAlbumArt = pCurPicture;
        }
    }

    if (pAlbumArt == NULL)
    {
        cerr << "No pictures found" << endl;
        return;
    }

    Blob coverImageBlob(pAlbumArt->picture().data(), pAlbumArt->size());
    Image coverImage(coverImageBlob);

    stringstream finalGeometry;
    finalGeometry << m_ThumbnailSize << "x" << m_ThumbnailSize;

    if (m_CoverOverlay)
    {
        stringstream geometryString;
        geometryString << coverWidth << "x" << coverHeight << "!";

        Blob cdCaseBlob(cdCaseData, sizeof(cdCaseData));
        Image cdCase(cdCaseBlob);

        coverImage.resize(Geometry(geometryString.str()));
        cdCase.composite(coverImage, coverImageOffsetX, coverImageOffsetY, PlusCompositeOp);
        cdCase.resize(Geometry(finalGeometry.str()));
        cdCase.write(outputFile);
    }
    else
    {
        coverImage.resize(Geometry(finalGeometry.str()));
        coverImage.write(outputFile);
    }
}
